package com.wjk.kylin.core.common.oss;


import org.apache.commons.lang3.StringUtils;
import org.springframework.util.Assert;

import java.io.File;

public abstract class AbstractOSSServer<T extends OSSFileModel> implements OSSServer<T>{

    @Override
    public String uploadFile(T fileModel) {
        Assert.notNull(fileModel.getFile(), "需要上传文件为空");

        //如果没有上传文件后缀，则获取文件名后缀
        if(StringUtils.isEmpty(fileModel.getExtName())){
            String fileName = fileModel.getFile().getOriginalFilename();
            fileModel.setExtName(fileName.substring(fileName.lastIndexOf(".") + 1));
        }

        return upload(fileModel);
    }

    @Override
    public File downloadFile(String filePath) {
        Assert.notNull(filePath, "需要下载文件为空");

        return download(filePath);
    }

    @Override
    public void deleteFile(String filePath) {
        Assert.notNull(filePath, "需要删除文件为空");

        delete(filePath);
    }

    protected abstract String upload(T ossFileModel);
    protected abstract void delete(String filePath);
    protected abstract File download(String filePath);



}
